﻿using Commen;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace WinService
{
   public class ServiceDo
    {
        public void DoT()
        {
            try
            {
                //实例化Timer类  
                System.Timers.Timer Timer1 = new System.Timers.Timer();
                // 调用本方法开始用计算器            
                // Timer1.Interval = 1000 * 60;//测试 
                var tt = DatHelper<double>.GetDat("Hz.dat");
                if (tt == 0)
                    tt = 12;
                Timer1.Interval = 1000 * 60 *  tt;//设置时钟周期为1秒（1000毫秒） 

                Log.Info($"Timer1.Interval：{Timer1.Interval}");
            
                Timer1.Elapsed += new ElapsedEventHandler(Timer1_Tick);
                Timer1.AutoReset = true;//执行一次 false，一直执行true  
                                        // Enable timer.  
                Timer1.Enabled = true;
            }

            catch (Exception ex)
            {
                Log.Error("DoT error: "+ex.Message + "");
            }
        }

        /// <summary>
        /// 运行程序的代码
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        private void Timer1_Tick(object source, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                var dt = DateTime.Now;

                #region 抓取服务

                #region 内容抓取

                var Spler = DatHelper<int>.GetDatList("Spler.dat");
                if (Spler == null || Spler.Count() == 0) { return; }
                Log.Info($"Spler:{fastJSON.JSON.Instance.ToJSON(Spler)}");
                if (Spler.Contains(dt.Hour)) { new SplerDo().Doto(); }
                #endregion


                #region fenci 分词处理
                var fenci = DatHelper<int>.GetDatList("Spler.dat");
                if (fenci == null || fenci.Count() == 0) { return; }
                Log.Info($"pangu--Spler:{fastJSON.JSON.Instance.ToJSON(fenci)}");
                if (fenci.Contains(dt.Hour - 1)) { new PanGuFenCi().ToDo(); }
                #endregion

                #region 关键字搜索
                var So = DatHelper<int>.GetDatList("So.dat");
                if (So == null || So.Count() == 0) { return; }
                Log.Info($"So:{fastJSON.JSON.Instance.ToJSON(So)}");
                if (So.Contains(dt.Hour)) { new SoDo().ToDo(); }
                #endregion


                #region 同步动作
                var Tonbu = DatHelper<int>.GetDatList("Tonbu.dat");
                if (Tonbu == null || Tonbu.Count() == 0) { return; }
                Log.Info($"Tonbu:{fastJSON.JSON.Instance.ToJSON(Tonbu)}");
                if (Tonbu.Contains(dt.Hour)) { new TonbuDo().ToDo(); }
                #endregion

                #endregion

            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
            }
        }


    }
}
